class NumberController < ApplicationController
  before_filter :sys_before_log

  #获取所有列车的信息
  def getNumbers
    total = Number.count
    numbers = Number.findPage params[:start].to_i,params[:limit].to_i
    jsonStr="{number:"+numbers.to_json()+",total:"+total.to_s+"}"
    render :text => jsonStr,:layout => false
  end

  def create
    begin
      number = Number.find_by_sn params[:sn]
      if number != nil
        info = '车次已存在，请更换！'
      else
        number = Number.new
        number.sn = params[:sn]
        number.line = params[:line]
        number.length = params[:length]
        number.save!
        info = 'success'
      end
    rescue Exception => e
      logger.info e
      info = '创建异常'
    end

    result = "{success:true,info:\"#{info}\"}"
    render :text => result,:layout => false
  end

  def delete
    begin
      ids= params[:id][1..params[:id].length-2].split(',')
      number.destroy ids
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '删除异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  def edit
    number = Number.find(params[:id])
    jsonStr = number.to_json()
    render :text=>jsonStr,:layout=>false
  end

  def update
    begin
      number = Number.find(params[:id])
      if number.sn == params[:sn].to_s
        number.line = params[:line]
        number.length = params[:length].to_i
        number.save
        info = 'success'
      else
        numberSN = Number.find_by_sn params[:sn]
        if numberSN == nil
          number.sn = params[:sn]
          number.line = params[:line]
          number.length = params[:length].to_i
          number.save
          info = 'success'
        else
          info = '该车次已存在，请更换！'
        end
      end
    rescue Exception=>e
      logger.info e
      info = '更新异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end
end
